import { _decorator, Component, error } from 'cc';
import { BaseCharacter } from '../BaseCharacter';
import { CharacterEvent } from '../CharacterEvent';
import { GameEnum } from '../GameEnum';
import { BaseBuff } from './BaseBuff';
import { BleedBuff } from './BleedBuff';
import IBaseBuffDTO from './IBaseBuffDTO';
import { PoisonBuff } from './PoisonBuff';
const { ccclass, property, requireComponent, disallowMultiple } = _decorator;

@ccclass('BuffController')
@requireComponent(BaseCharacter)
@disallowMultiple(true)
export class BuffController extends Component {
    /****************************************************************************************************************
     *Readonly 常量
     ****************************************************************************************************************/
    /****************************************************************************************************************
     *Property 定义变量
     ****************************************************************************************************************/
    /***************************************************************
     *PropertyUI 引用的控件
     ***************************************************************/
    /**
     * 人物组件
     */
    private character: BaseCharacter = undefined!;
    /****************************************************************************************************************
     * Lifecycle 生命周期相关方法
     ****************************************************************************************************************/
    protected onLoad(): void {
        if (!this.character) {
            this.character = this.node.getComponent(BaseCharacter);
        }
        if (!this.character) {
            error(`BUFF控制器获取人物组件失败`)
            return;
        }
        this.node.on(CharacterEvent.DEAD, this.onCharacterDead, this);
    }
    /****************************************************************************************************************
     * Public 公有方法
     ****************************************************************************************************************/
    /**
     * 应用BUFF
     * @param value 
     */
    public applyBuff(value: IBaseBuffDTO): void {
        let type = value.id;

        let buff: BaseBuff = undefined!;
        switch (type) {
            case GameEnum.BuffType.BLEED:
                buff = this.node.getComponent(BleedBuff);
                if (!buff) {
                    buff = this.node.addComponent(BleedBuff);
                }
                break;
            case GameEnum.BuffType.POISON:
                buff = this.node.getComponent(PoisonBuff);
                if (!buff) {
                    buff = this.node.addComponent(PoisonBuff);
                }
                break;
            default:
                error(`BUFF控制器未定义的BUFF类型${value.id}`)
                break;
        }

        buff.applyBuff(value);
    }
    /****************************************************************************************************************
     * private 私有方法
     ****************************************************************************************************************/
    /**
     * 战斗单位死亡时移除所有BUFF
     */
    private onCharacterDead() {
        let buffs = this.node.getComponents(BaseBuff);
        buffs.forEach((buff) => {
            buff.enabled = false;
        })
    }
    /****************************************************************************************************************
     * Property Get && Set 属性的 get && set 函数
     ****************************************************************************************************************/

}


